/*!
 * @文件名 logic_diagnose.c
 *
 * @功能描述
 *	通过GPIO以及寄存器,来检测max25512,pmic,tft的故障原因
 */
#include "logic_public.h"
#include "shell.h"

extern task_t s_logic_check_wakeup;
extern task_t s_logic_check_lvds;
extern task_t s_bist_check_task;
static task_t s_diagnose_task;

#define ADC_SAMPLE_CNT		10							//ADC采样次数
static uint16_t g_pAdcBattArray[ADC_SAMPLE_CNT]={0};	//电压AD采集BUF
static uint16_t g_AdcBatVoltage = 0;					//B+电压,供其它模块判断使用

ST_DIAGNOSE1_DATA g_err_status1;
ST_DIAGNOSE2_DATA g_err_status2;
uint8_t	result1,result2;

/*FUNCTION**********************************************************************
 *
 * Function Name : disgnose_batt_voltage_process
 * Description   : 采集B+电压,处理高低压异常
 *
 *END**************************************************************************/
void disgnose_batt_voltage_process(void)
{
	uint16_t iBattAd,iBattVoltage=0;
	static int i = 0;
	static eNotifyEvent voltageStatusNow = EN_NOTIFY_VOLTAGE_NORMAL;
	static eNotifyEvent voltageStatusOld = EN_NOTIFY_VOLTAGE_NORMAL;
	g_pAdcBattArray[i++] = fml_adc_read_bat_ad();
	if(i==ADC_SAMPLE_CNT)
	{
		i = 0;
		iBattAd	= fml_adc_filter(g_pAdcBattArray,ADC_SAMPLE_CNT);
		g_AdcBatVoltage = iBattVoltage = fml_adc_to_bat_voltage(iBattAd);
		//logWrite("iBattVoltage=%d\r\n",iBattVoltage);
		if(iBattVoltage > VOLTAGE_HIGH_IN)
		{
			g_err_status1.HIGH_VOL_FAIL = 1;
			voltageStatusNow = EN_NOTIFY_VOLTAGE_HIGH;
		}
		else if(iBattVoltage < VOLTAGE_LOW_IN)
		{
			g_err_status1.LOW_VOL_FAIL = 1;
			voltageStatusNow = EN_NOTIFY_VOLTAGE_LOW;
		}
		else if(iBattVoltage <= VOLTAGE_NORMAL_MAX && iBattVoltage >= VOLTAGE_NORMAL_MIN)
		{
			g_err_status1.HIGH_VOL_FAIL = 0;
			g_err_status1.LOW_VOL_FAIL = 0;
			voltageStatusNow = EN_NOTIFY_VOLTAGE_NORMAL;

		}
		if(voltageStatusOld != voltageStatusNow)
		{
			voltageStatusOld = voltageStatusNow;
			cola_set_event(&s_logic_check_wakeup,voltageStatusNow);
		}
	}
}


/*FUNCTION**********************************************************************
 *
 * Function Name : diagnose_polling_task
 * Description   : 诊断任务处理函数
 *
 *END**************************************************************************/
void diagnose_polling_task(void *arg, uint32_t event)
{
	(void)arg;
	(void)event;
	disgnose_batt_voltage_process();
	fml_ntc_temperature_process();
	g_err_status1.LCD_FAIL 		= fml_gpio_read(IN_TFT_ERR);
	g_err_status1.BACKLIGHT_FAIL= !fml_gpio_read(IN_BL_ERR);
	g_err_status2.LOCK_FAIL 	= !fml_gpio_read(IN_LVDS_UNLOCK);
	g_err_status2.PMIC_FAIL 	= !fml_gpio_read(IN_PMIC_FLTB);
	g_err_status2.LDO_1V2_FAIL 	= !fml_gpio_read(IN_LDO_1V2_PG);
	g_err_status2.SW_3V3_FAIL 	= !fml_gpio_read(IN_SW_3V3_PG);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : logic_diagnose_init
 * Description   : 初始化诊断检测任务
 *
 *END**************************************************************************/
void logic_diagnose_init(void)
{
	cola_timer_create(&s_diagnose_task, diagnose_polling_task, NULL);
	cola_timer_start(&s_diagnose_task, EN_COLA_TIMER_ALWAYS, 100);
}


void logic_diagnose_print(void)
{
    logWrite("g_err_status1:\r\n");
	logWrite("LOW_VOL_FAIL=%d\r\n",g_err_status1.LOW_VOL_FAIL);
	logWrite("HIGH_VOL_FAIL=%d\r\n",g_err_status1.HIGH_VOL_FAIL);
	logWrite("LCD_FAIL=%d\r\n",g_err_status1.LCD_FAIL);
	logWrite("BACKLIGHT_FAIL=%d\r\n",g_err_status1.BACKLIGHT_FAIL);
	logWrite("LCD_TEMPERATURE_HIGH=%d\r\n",g_err_status1.LCD_TEMPERATURE_HIGH);
	logWrite("LCD_NTC_FAIL=%d\r\n",g_err_status1.LCD_NTC_FAIL);
	logWrite("RESERVER6=%d\r\n",g_err_status1.RESERVER6);
	logWrite("RESERVER7=%d\r\n",g_err_status1.RESERVER7);

	logWrite("\r\n");

	logWrite("g_err_status2:\r\n");
	logWrite("ENABLE_SIGNAL_FAIL=%d\r\n",g_err_status2.ENABLE_SIGNAL_FAIL);
	logWrite("MASTER_IIC_FAIL=%d\r\n",g_err_status2.MASTER_IIC_FAIL);
	logWrite("LOCK_FAIL=%d\r\n",g_err_status2.LOCK_FAIL);
	logWrite("TOUCH_IIC_FAIL=%d\r\n",g_err_status2.TOUCH_IIC_FAIL);
	logWrite("LCD_TEMPERAWATCH_DOG_FAILTURE_HIGH=%d\r\n",g_err_status2.WATCH_DOG_FAIL);
	logWrite("PMIC_FAIL=%d\r\n",g_err_status2.PMIC_FAIL);
	logWrite("LDO_1V2_FAIL=%d\r\n",g_err_status2.LDO_1V2_FAIL);
	logWrite("SW_3V3_FAIL=%d\r\n",g_err_status2.SW_3V3_FAIL);
}


#if 1
int diagnose(int argc, char *argv[])
{
	if (argc < 3)
	{
		logWrite("para length must greater than 3\r\n");
		return 0;
	}

	int mode = atoi(argv[1]);
	//int value = atoi(argv[2]);
	if(mode == 0)
	{
		fml_max25512_read_diagnose();
	}
	else if(mode == 1)
	{
		logic_diagnose_print();
	}
	return 0;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), diagnose, diagnose, diagnose,diagnose);
#endif
